package com.icss.ah.fullReturn.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FullReturnService {
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	/**
	 * 满返活动列表查询
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getFullReturnList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("FullReturnBean.getFullReturnList",map);
			infolog.info("FullReturnService|getFullReturnList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReturnService|getFullReturnList", e);
		}
		return list;
	}
	
	/**
	 * 满返活动统计
	 * @param map
	 * @return
	 */
	public Long getFullReturnListCount(Map<String, String> map){
		Long count = 0L;
		try {
			count = sqlSessionTemplate.selectOne("FullReturnBean.getFullReturnListCount",map);
			infolog.info("FullReturnService|getFullReturnListCount|count: "+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReturnService|getFullReturnListCount", e);
		}
		return count;
	}
	
	/**
	 * 满返活动保存
	 * @param map
	 */
	public void saveInfo(Map<String, String> map){
		try {
			sqlSessionTemplate.insert("FullReturnBean.saveInfo",map);
			infolog.info("FullReturnService|saveInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReturnService|saveInfo", e);
		}
	}
	
	/**
	 * 满返活动编辑
	 * @param map
	 */
	public void updateInfo(Map<String, String> map){
		try {
			sqlSessionTemplate.update("FullReturnBean.updateInfo",map);
			infolog.info("FullReturnService|updateInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReturnService|updateInfo", e);
		}
	}
	
	/**
	 * 满返活动状态启用/禁用
	 * @param rtnId
	 */
	public void updateStatus(String rtnId){
		try {
			sqlSessionTemplate.update("FullReturnBean.updateStatus",rtnId);
			infolog.info("FullReturnService|updateStatus");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReturnService|updateStatus", e);
		}
	}
	
	/**
	 * 满返活动详情
	 * @param rtnId
	 * @return
	 */
	public Map<String, Object> getFullReturnInfo(String rtnId){
		Map<String, Object> info = new HashMap<String, Object>();
		try {
			info = sqlSessionTemplate.selectOne("FullReturnBean.getFullReturnInfo",rtnId);
			infolog.info("FullReturnService|getFullReturnInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReturnService|getFullReturnInfo", e);
		}
		return info;
	}
	
	/**
	 * 查询代金券
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getCouponsList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("FullReturnBean.getCouponsList",map);
			infolog.info("FullReturnService|getCouponsList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReturnService|getCouponsList", e);
		}
		return list;
	}
	
	/**
	 * 删除满返信息
	 * @param rdeId
	 * @return
	 */
	public String delInfo(String rtnId){
		String result = "f";
		try {
//			sqlSessionTemplate.delete("FullReturnBean.delInfo",rtnId);
			sqlSessionTemplate.update("FullReturnBean.upInfo",rtnId);
			result = "t";
			infolog.info("FullReturnService|delInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReturnService|delInfo", e);
		}
		return result;
	}

}
